package com.example.designpattern.responsibility_two;

import com.example.model.GoodsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;

import java.util.Comparator;
import java.util.List;

/**
 * 将所有的实现类都注入，然后在初始化方法中组装顺序
 * 实现Order接口，按照优先级顺序设置下一个节点
 * @author long
 * @date 2024/12/20 14:49
 */
//@Service
public class ParamValidatorChainByAutowired{

    @Autowired
    private List<IValidator> validatorList;

//    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        validatorList.sort(Comparator.comparingInt(Ordered::getOrder));
        for(int index = 0;index < validatorList.size();index++){
            // 不是最后一个节点
            if(index != validatorList.size() -1){
                IValidator curValidator = validatorList.get(index);
                curValidator.setNext(validatorList.get(index+1));
            }
        }
    }

    public void startParamValidate(GoodsDTO dto){
        // 链式调用校验
        validatorList.get(0).validate(dto);

        // todo 后续的业务校验
    }

}
